﻿using CSharpDay17__API_with_Angular_.Data.Model;
using Microsoft.AspNet.Identity;
using Microsoft.AspNet.Identity.EntityFramework;
using System;
using System.Collections.Generic;
using System.Data.Entity.Migrations;
using System.Linq;
using System.Text;
using System.Threading.Tasks;

namespace CSharpDay17__API_with_Angular_.Data
{
    public static class Seeder
    {
        public static void Seed(ApplicationDbContext db, bool roles = true, bool users = true, bool notes = true)
        {
            if (roles) SeedRoles(db);
            if (users) SeedUsers(db);
            if (notes) SeedNotes(db);
        }

        private static void SeedRoles(ApplicationDbContext db)
        {
            var manager = new RoleManager<IdentityRole>(new RoleStore<IdentityRole>(db));

            if (!manager.RoleExists("User")) manager.Create(new IdentityRole() { Name = "User" });
            if (!manager.RoleExists("Admin")) manager.Create(new IdentityRole() { Name = "Admin" });
        }

        private static void SeedUsers(ApplicationDbContext db)
        {
            var manager = new UserManager<ApplicationUser>(new UserStore<ApplicationUser>(db));

            if (!db.Users.Any(x => x.UserName == "test"))
            {
                ApplicationUser user = new ApplicationUser()
                {
                    UserName = "test",
                    Email = "test@test.com"
                };
                manager.Create(user, "123123");
                manager.AddToRole(user.Id, "User");
            }
            if (!db.Users.Any(x => x.UserName == "admin"))
            {
                ApplicationUser user = new ApplicationUser()
                {
                    UserName = "admin",
                    Email = "admin@test.com"
                };
                manager.Create(user, "123123");
                manager.AddToRole(user.Id, "Admin");
            }
        }

        private static void SeedNotes(ApplicationDbContext db)
        {
            string user = db.Users.FirstOrDefault(x => x.UserName == "test").Id;
            string admin = db.Users.FirstOrDefault(x => x.UserName == "admin").Id;

            db.Notes.AddOrUpdate(x => x.Id,
                new Note() { Id = 1, UserId = user, DateCreated = DateTime.Now, Description = "Hello 1" },
                new Note() { Id = 2, UserId = user, DateCreated = DateTime.Now, Description = "Hello 2", DateDeleted = DateTime.Now },
                new Note() { Id = 3, UserId = user, DateCreated = DateTime.Now, Description = "Hello 3" },
                new Note() { Id = 4, UserId = admin, DateCreated = DateTime.Now, Description = "Hello 4", DateDeleted = DateTime.Now },
                new Note() { Id = 5, UserId = admin, DateCreated = DateTime.Now, Description = "Hello 5" },
                new Note() { Id = 6, UserId = admin, DateCreated = DateTime.Now, Description = "Hello 6", DateDeleted = DateTime.Now }
            );
        }
    }
}